package com.kui.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kui.entity.User;

public class WebFilter implements Filter{
	private String value = "UTF-8";
	
	public void destroy() {
		
	}

	public void doFilter(ServletRequest a, ServletResponse b,
			FilterChain fc) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) a;
		HttpServletResponse response = (HttpServletResponse) b;
		
		request.setCharacterEncoding(value);
		String url = request.getRequestURI();
		HttpSession session = request.getSession();
		
		if(url.indexOf(".css") != -1 || url.indexOf("jquery.js") != -1 || url.indexOf(".min.js") != -1 || url.indexOf(".png") != -1 || url.indexOf("index.jsp") != -1 || url.indexOf("index.jspx") != -1 || url.indexOf(".gif") != -1 || url.indexOf("sign.jspx") != -1 || url.indexOf(".jpg") != -1 ) {
			fc.doFilter(request, response);
		}else {
			User user = (User) session.getAttribute("user");
			if(user != null){
				fc.doFilter(request, response);
				
			}else{
				response.sendRedirect("index.jspx");
			}
		}
		
		
	}

	public void init(FilterConfig fc) throws ServletException {
		String encoding = fc.getInitParameter("encoding");
		if(encoding != null || !("".equals(encoding))){
			value = encoding;
		}
	}

}
